<template>
  <div class="pinch-game">
    <!-- 顶部导航 -->
    <div class="game-header">
      <h1>捏脸小游戏</h1>
      <div class="score-container">
        <div class="score-label">得分</div>
        <div class="score-value">{{ score }}</div>
        <transition name="pop">
          <div v-if="showFeedback" class="score-feedback">+{{ lastScore }}</div>
        </transition>
      </div>
    </div>

    <!-- 游戏区域 -->
    <div class="character-container">
      <div class="character">
        <!-- 头发 -->
        <div class="hair">
          <div class="hair-front"></div>
          <div class="hair-side left"></div>
          <div class="hair-side right"></div>
        </div>
        
        <!-- 脸部 -->
        <div
          class="face"
          @mousedown="startPinch"
          @mousemove="handlePinch"
          @mouseup="endPinch"
          @mouseleave="endPinch"
          :style="faceStyle"
          ref="face"
        >
          <!-- 眼睛 -->
          <div class="eyes">
            <div class="eye left">
              <div class="pupil"></div>
            </div>
            <div class="eye right">
              <div class="pupil"></div>
            </div>
          </div>
          
          <!-- 嘴巴 -->
          <div class="mouth"></div>
          
          <!-- 脸红效果 -->
          <div class="blush left"></div>
          <div class="blush right"></div>
        </div>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons">
      <button class="btn-primary" @click="enterLianLianKan">
        <span>玩连连看</span>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M5 12h14M12 5l7 7-7 7"/>
        </svg>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SakikoCharacter',
  data() {
    return {
      isPinching: false,
      startX: 0,
      startY: 0,
      currentX: 0,
      currentY: 0,
      score: 0,
      lastScore: 0,
      showFeedback: false
    }
  },
  computed: {
    faceStyle() {
      if (!this.isPinching) {
        return { 
          clipPath: 'circle(50% at 50% 50%)',
          transform: 'translate(0, 0)'
        }
      }

      const dx = this.currentX - this.startX
      const dy = this.currentY - this.startY
      const distance = Math.sqrt(dx * dx + dy * dy)
      const angle = Math.atan2(dy, dx)

      const pullStrength = Math.min(distance / 50, 1.5)
      const pullX = 50 + Math.cos(angle) * pullStrength * 20
      const pullY = 50 + Math.sin(angle) * pullStrength * 20

      return {
        clipPath: `
          circle(50% at 50% 50%)
          ellipse(${pullX}% ${pullY}% at 50% 50%)
        `,
        transform: `translate(${dx * 0.2}px, ${dy * 0.2}px)`,
        transition: 'none'
      }
    }
  },
  methods: {
    startPinch(e) {
      this.isPinching = true
      const rect = this.$refs.face.getBoundingClientRect()
      this.startX = e.clientX - rect.left
      this.startY = e.clientY - rect.top
      this.currentX = this.startX
      this.currentY = this.startY
    },
    handlePinch(e) {
      if (!this.isPinching) return
      const rect = this.$refs.face.getBoundingClientRect()
      this.currentX = e.clientX - rect.left
      this.currentY = e.clientY - rect.top
    },
    endPinch() {
      if (!this.isPinching) return
      this.isPinching = false

      const dx = this.currentX - this.startX
      const dy = this.currentY - this.startY
      const distance = Math.sqrt(dx * dx + dy * dy)

      this.lastScore = Math.floor(distance * 0.2)
      this.score += this.lastScore

      this.showFeedback = true
      setTimeout(() => {
        this.showFeedback = false
      }, 800)
    },
    enterLianLianKan() {
      this.$emit('go-to-lianliankan')
    }
  }
}
</script>

<style scoped>
.pinch-game {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.game-header h1 {
  color: #3a3a3a;
  font-size: 1.8rem;
  font-weight: 700;
  text-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}

.score-container {
  position: relative;
  background: white;
  border-radius: 20px;
  padding: 8px 16px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
}

.score-label {
  color: #888;
  font-size: 0.9rem;
  margin-right: 8px;
}

.score-value {
  color: #FF5722;
  font-weight: 700;
  font-size: 1.2rem;
}

.score-feedback {
  position: absolute;
  right: -30px;
  color: #4CAF50;
  font-weight: bold;
  font-size: 1rem;
  animation: floatUp 0.8s ease-out forwards;
}

@keyframes floatUp {
  0% {
    opacity: 1;
    transform: translateY(0);
  }
  100% {
    opacity: 0;
    transform: translateY(-30px);
  }
}

.character-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.character {
  position: relative;
  width: 250px;
  height: 300px;
}

.hair {
  position: absolute;
  width: 180px;
  height: 180px;
  background: #3E2723;
  border-radius: 50% 50% 40% 40%;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1;
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.hair-front {
  position: absolute;
  width: 180px;
  height: 60px;
  background: #3E2723;
  border-radius: 50% 50% 0 0;
  top: -20px;
}

.hair-side {
  position: absolute;
  width: 40px;
  height: 60px;
  background: #3E2723;
  top: 30px;
}

.hair-side.left {
  left: -20px;
  border-radius: 20px 0 0 20px;
}

.hair-side.right {
  right: -20px;
  border-radius: 0 20px 20px 0;
}

.face {
  position: absolute;
  width: 150px;
  height: 180px;
  background: #FFD5C2;
  border-radius: 50%;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  box-shadow: 
    0 5px 15px rgba(0,0,0,0.1),
    inset 0 -5px 10px rgba(0,0,0,0.05);
  transition: all 0.3s ease;
  cursor: grab;
}

.face:active {
  cursor: grabbing;
}

.eyes {
  position: absolute;
  top: 70px;
  width: 100%;
  display: flex;
  justify-content: space-around;
}

.eye {
  width: 30px;
  height: 20px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.eye::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background: rgba(0,0,0,0.1);
  top: 50%;
  transform: translateY(-50%);
}

.pupil {
  width: 12px;
  height: 12px;
  background: #3E2723;
  border-radius: 50%;
  transition: all 0.1s ease;
}

.mouth {
  position: absolute;
  width: 40px;
  height: 15px;
  background: #E91E63;
  border-radius: 0 0 20px 20px;
  bottom: 50px;
  left: 50%;
  transform: translateX(-50%);
  transition: all 0.2s ease;
}

.blush {
  position: absolute;
  width: 25px;
  height: 15px;
  background: rgba(255,105,180,0.3);
  border-radius: 50%;
  top: 90px;
}

.blush.left {
  left: 20px;
}

.blush.right {
  right: 20px;
}

.action-buttons {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

.btn-primary {
  display: flex;
  align-items: center;
  gap: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 30px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 15px rgba(118, 75, 162, 0.3);
  transition: all 0.3s ease;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(118, 75, 162, 0.4);
}

.btn-primary svg {
  width: 18px;
  height: 18px;
}

.pop-enter-active,
.pop-leave-active {
  transition: all 0.5s ease;
}

.pop-enter-from,
.pop-leave-to {
  opacity: 0;
  transform: scale(0.5);
}
</style>